package com.scorpionstudio.dantripro;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.scorpionstudio.utils.Utils;

public class SplashActivity extends Activity {
	private Handler mHandler = new Handler();
	private Runnable mRunnable;
	private static int SPLASH_TIME_OUT = 4000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_splash);
		mRunnable = new Runnable() {
			@Override
			public void run() {
				checkNetWork();
			}
		};
		mHandler.postDelayed(mRunnable,SPLASH_TIME_OUT);
	}

	private void checkNetWork() {
		if (Utils.isOnline(SplashActivity.this) == true) {
			Intent i = new Intent(SplashActivity.this, Main.class);
			startActivity(i);
			finish();
		} else {
			final Dialog dialog = new Dialog(SplashActivity.this);
			dialog.setContentView(R.layout.layout_custome_dialog);
			dialog.setTitle("Error Network");
			dialog.setCancelable(true);
			dialog.setCanceledOnTouchOutside(false);
			dialog.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					dialog.dismiss();
					finish();
				}
			});
			TextView text = (TextView) dialog
					.findViewById(R.id.tv_dialog_content);
			text.setText("Not connect network in your phone. please check config.");
			Button btnOK = (Button) dialog.findViewById(R.id.btn_ok);
			btnOK.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS),100);
				}
			});
			Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel);
			btnCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
					finish();
				}
			});

			dialog.show();
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		mHandler.postDelayed(mRunnable,SPLASH_TIME_OUT);
	}
	@Override
	public void onBackPressed() {
		mHandler.removeCallbacks(mRunnable);
		finish();
	}

}
